package com.oxygen.pt.mapper;

import com.oxygen.pt.pojo.entity.Patient;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface PatientMapper {

    /**
     * 新增患者 ，插入新的患者数据
     * @param patient
     */
    @Insert("INSERT INTO patient (patient_id, name, age, phone, gender, cost_type, person_type, " +
            "diagnosis_info, treatment_plan, plan_sessions, plan_pressure, treatment_status, " +
            "remarks, treatment_records_id, create_time, update_time) " +
            "VALUES (#{patientId}, #{name}, #{age}, #{phone}, #{gender}, #{costType}, #{personType}, " +
            "#{diagnosisInfo}, #{treatmentPlan}, #{planSessions}, #{planPressure}, #{treatmentStatus}, " +
            "#{remarks}, #{treatmentRecordsId}, #{createTime}, #{updateTime})")
    void add(Patient patient);

    /**
     * 分页查询带筛选功能
     * @param costType 费别
     * @param gender 性别
     * @param minAge 年龄范围 低值
     * @param maxAge 年龄范围 高值
     * @param personType  人员类型
     * @return PageHelper进行数据返回
     */
   List<Patient> selectPatients(String costType, Integer gender, Long minAge, Long maxAge, Long personType);

    /**
     * 根据患者的ID返回患者信息
     * @param id 患者id
     * @return 患者信息详情
     */
   @Select("SELECT * FROM patient where patient_id = #{id}")
   Patient getById(Long id);

   @Select("SELECT treatment_status FROM patient where patient_id = #{id}")
   Integer getStatusById(Long id);

    /**
     * 查询一段时间内的患者人数，主要服务与统计该季度与上一季度的患者数量的上涨还是下降
     * @param startDate 日期最早
     * @param endDate 日期最晚
     * @return 患者人数
     */
    @Select("SELECT COUNT(*) FROM patient WHERE create_time BETWEEN #{startDate} AND #{endDate}")
    Long getNumByDate(LocalDate startDate, LocalDate endDate);

    @Select("SELECT COUNT(*) FROM patient " +
            "WHERE treatment_status = 4 " +
            "AND create_time BETWEEN #{startDate} AND #{endDate}")
    Long getNumByStatus(LocalDate startDate, LocalDate endDate);

    /**
     * 统计患者类型的数量
     * @param personType
     * @return
     */
    int getByPersonType(@Param("personType") Integer personType);

    /**
     * 更新患者状态
     * @param id 需要更新的患者的id
     */
    @Update("UPDATE patient SET treatment_status=#{treatmentStatus}, update_time=NOW() WHERE patient_id=#{patientId}")
    void updateStatus(@Param("patientId") Long id, @Param("treatmentStatus") int  treatmentStatus);

    /**
     * 根据患者状态统计该状态下的患者人数
     * @param treatmentStatus 患者状态
     * @return 状态下的患者数量
     */
    @Select("SELECT COUNT(*) FROM patient WHERE treatment_status = #{treatmentStatus}")
    int countPatientsByStatus(@Param("treatmentStatus") int treatmentStatus);

    @Update("UPDATE patient SET deleted = 1 WHERE patient_id = #{patientId} AND deleted = 0")
    void deleteById(@Param("patientId") Long patientId);

    int updatePatient(Patient patient);
}
